package com.spring.shiro.service;

import com.spring.shiro.entity.RoleMenu;
import com.spring.shiro.mapper.RoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

@Service
public class RoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    public void deleteRoleMenusByRoleId(String roleIds) {
        List<String> list = Arrays.asList(roleIds.split(","));

        this.batchDelete(list,"roleId",RoleMenu.class);
    }

    public void deleteRoleMenusByMenuId(String menuIds) {
        List<String> list = Arrays.asList(menuIds.split(","));

        this.batchDelete(list,"menuId",RoleMenu.class);
    }

    @Transactional
    public int batchDelete(List<String> list, String property, Class clazz) {
        Example example = new Example(clazz);
        example.createCriteria().andIn(property, list);
        return this.roleMenuMapper.deleteByExample(example);
    }

}
